package com.tcm.login.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.tcm.user.model.query.LoginAuthQuery;
import com.tcm.user.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    Logger log = LoggerFactory.getLogger(UserController.class);
    @DubboReference(version = "1.0.0", check = false, timeout = 3000)
    UserService userService;

    /**
     * 修改登录手机号
     */
    @PostMapping("/update/mobile")
    public R changeMobile(@RequestBody LoginAuthQuery loginAuthQuery) {
        try {
            return userService.changeMobile(loginAuthQuery);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改登录密码
     */
    @PostMapping("/update/password")
    public R changePassword(@RequestBody LoginAuthQuery loginAuthQuery) {
        try {
            return userService.changeCredential(loginAuthQuery);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改登录用户名
     */
    @PostMapping("/update/username")
    public R updateUsername(@RequestBody LoginAuthQuery loginAuthQuery) {
        try {
            return userService.changeUsername(loginAuthQuery);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询登录信息
     */
    @GetMapping("/query/userId")
    public R findUserById(Long userId, String clientType) {
        try {
            return userService.findByUserId(userId, clientType);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询登录信息
     */
    @GetMapping("/query/mobile")
    public R findUserByMobile(String mobile, String clientType) {
        try {
            return userService.findByMobile(mobile, clientType);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询登录信息
     */
    @GetMapping("/query/username")
    public R findUserByUsername(String username, String clientType) {
        try {
            return userService.findByUsername(username, clientType);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }


}
